package com.chamei.compiler;

import com.chamei.annotation.ARouter;
import com.chamei.annotation.HelloWorld;
import com.chamei.compiler.utils.Constants;
import com.google.auto.service.AutoService;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

import java.io.IOException;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;


@AutoService(Processor.class)
// 接收的类型
@SupportedAnnotationTypes({Constants.HELLO_WORLD_ANNOTATION_TYPES})
// JDK的版本
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class HelloWorldProcessor extends AbstractProcessor {

    // 操作Element工具类
    private Elements elementsUtils;
    // 打印日志
    private Messager messager;
    // 文件生成器
    private Filer filer;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnvironment) {
        super.init(processingEnvironment);
        // 初始化工作
        elementsUtils = processingEnvironment.getElementUtils();
        messager = processingEnvironment.getMessager();
        filer = processingEnvironment.getFiler();
        messager.printMessage(Diagnostic.Kind.NOTE,"注解处理器初始化完成 >>>>>>>>>>>>");
    }

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        if(set.isEmpty()) return false;
        // 获取所有带有ARouter注解的类节点
        Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(HelloWorld.class);
        if(elements.isEmpty()) return false;
        try {
            parseElements(elements);
        }catch (IOException ioe){
            ioe.printStackTrace();
        }
        return true;
    }

    public void parseElements(Set<? extends Element> elements) throws IOException{
        // 方法
        MethodSpec mainMethod = MethodSpec.methodBuilder("main")
                .addModifiers(Modifier.PUBLIC,Modifier.STATIC)
                .returns(void.class)
                .addParameter(String[].class,"args")
                .addStatement("$T.out.println($S)",System.class,"Hello JavaPoet")
                .build();
        // 类
        TypeSpec helloWorld = TypeSpec.classBuilder("Hello$$world")
                .addModifiers(Modifier.PUBLIC,Modifier.FINAL)
                .addMethod(mainMethod)
                .build();
        // 文件生成器
        JavaFile javaFile = JavaFile.builder("com.chamei.builder",helloWorld)
                .build();
        javaFile.writeTo(filer);
        messager.printMessage(Diagnostic.Kind.NOTE,"文件生成结束>>>>>>>>>>>>>");
    }
}
